package cn.wyj.service.impl;

import cn.wyj.dao.CommentMapper;
import cn.wyj.dao.StatisticMapper;
import cn.wyj.domain.Comment;
import cn.wyj.domain.Statistic;
import cn.wyj.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 获取文章对应评论信息
     * @param id
     * @param currentPage
     * @param pageCount
     * @return
     */
    @Override
    public PageInfo<Comment> getComments(Integer id, Integer currentPage, Integer pageCount) {
        PageHelper.startPage(currentPage,pageCount);
        List<Comment> comments = commentMapper.findCommentByPage(id);
        PageInfo<Comment> commentList = new PageInfo<>(comments);
        return commentList;
    }

    /**
     * 发表评论
     * @param comment
     */
    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
        Statistic statistic = statisticMapper.findStatisticByArticleId(comment.getArticleId());
        statistic.setCommentsNum(statistic.getCommentsNum()+1);
        statisticMapper.updateArticleCommentsByArticleId(statistic);
    }

    /**
     * 删除文章评论
     * @param articleId
     */
    @Override
    public void delComment(Integer articleId) {
        commentMapper.delCommentByAid(articleId);
    }
}
